package com.lbj.service;

import com.lbj.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service /*注解创建对象*/             /*事务传播行为,隔离级别*/
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)
/*事务注解可在类上面也可以在方法上*/
public class UserService {
    /*第一步 开启事务*/


    /*第二步 进行业务操作*/
    /*注入dao*/
    @Autowired
    private UserDao userDao;

    /*转账的方法*/
    public void accountMoney(){
        //try {
            //lucy少100
            userDao.reduceMoney();

            //模拟异常
            int i = 1/0;

            //mary多100
            userDao.addMoney();

            /*第三步 没有异常，提交事务*/


       // }catch(Exception e){
            /*第四步 出现异常，事务回滚*/
       // }
    }

}
